<template>
	<view class="main-page">
		<view class="header-place"></view>
		<view class="header-box">
			<image class="back-ic" src="../../static/common/ic_back.png" @click="goBack"></image>
			<view class="input-box">
				<image class="wh-22 ml-20 mr-8" src="../../static/common/ic_search.png"></image>
				<input 
					v-model="keyword"
					class="font-333-28"
					placeholder="请输入客户名称或电话"
					placeholder-class="font-999-28"
					style="width: 280rpx;"
					@input="handleInputConfirm"
				/>
				<image class="wh-34 ml-20" src="../../static/common/ic_clear.png" @click="keyword = ''"></image>
			</view>
		</view>
		
		<view class="column-container width-100p" v-if="dataList.length > 0">
			<view class="row-container ml-30 mt-30 mb-20">
				<text class="font-666-28">与搜索词</text>
				<text class="font-333-28 font-weight-600">“{{showKeyword}}”</text>
				<text class="font-666-28">相关的客户</text>
			</view>
			
			<view class="list-item" v-for="(item , index) in dataList" :key="index">
				<text class="font-333-26" v-if="item.lastTime">上次到店时间：{{item.lastTime}}</text>
				<text class="font-333-26" v-else>暂无到店记录</text>
				<view class="row-container mt-20">
					<image class="avatar" src="../../static/common/ic_avatar_female.png" v-if="item.sex == 0"></image>
					<image class="avatar" src="../../static/common/ic_avatar_male.png" v-if="item.sex == 1"></image>
					<view class="column-container">
						<text class="font-333-32 font-weight-600">{{item.realName}}</text>
						<view class="row-container mt-4">
							<text class="font-999-26">ID：{{item.id}}</text>
							<view class="column-line"></view>
							<text class="font-999-26" v-if="item.sex == 1">男</text>
							<text class="font-999-26" v-if="item.sex == 0">女</text>
							<view class="column-line"></view>
							<text class="font-999-26">联系电话：{{item.mobile}}</text>
						</view>
					</view>
				</view>
				<view class="row-container-row-between width-100p mt-30">
					<view class="row-container" @click="gotoEdit(item)">
						<image class="wh-28 mr-10" src="../../static/common/ic_edit.png"></image>
						<text class="font-666-28">编辑</text>
					</view>
					<view class="row-container">
						<text class="grey-box mr-20" @click="gotoRecord(item)">预约记录</text>
						<text class="grey-box mr-20" @click="gotoAppointment(item)">添加预约</text>
						<text class="grey-box" style="color: white;background: #F94C4B;">联系客户</text>
					</view>
				</view>
				
			</view>
		</view>
		
		<view class="column-container-center width-100p" v-if="showNoData">
			<image class="empty-ic" src="../../static/common/ic_no_client.png"></image>
			<view class="row-container">
				<text class="font-666-28">很抱歉，暂无与搜索词</text>
				<text class="font-333-28 font-weight-600">“{{showKeyword}}”</text>
				<text class="font-666-28">相关的客户</text>
			</view>
		</view>
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showNoData:false,
				
				keyword:'',
				showKeyword:'',
				
				dataList:[],
				page:1,
				hasMoreData:false,
				
			}
		},
		
		onPullDownRefresh() {
			if (this.keyword){
				this.page = 1
				this.loadDataList()
			}
		},
		
		onReachBottom(){
			if (this.keyword && this.hasMoreData){
				this.page ++
				this.loadDataList()
			}
		},
		
		methods: {
			goBack(){
				uni.navigateBack()
			},
			
			gotoEdit(item){
				uni.navigateTo({
					url:'/pages/client/editClient?customId=' + item.id
				})
			},
			
			gotoRecord(item){
				uni.navigateTo({
					url:'/pages/client/record?customId=' + item.id
				})
			},
			
			gotoAppointment(item){
				uni.$emit('chosenClient' , item)
				uni.switchTab({
					url:'/pages/appointment/index'
				})
			},
			
			handleInputConfirm(e){
				this.keyword = e.detail.value
				this.page = 1
				this.loadDataList()
			},
			
			loadDataList(){
				if (!this.keyword) return
				
				let that = this
				this.http({
					url:'/user/custom/findCustomList',
					method:'POST',
					data:{
						pageNo: that.page,
						pageSize: 20,
						keyWord:that.keyword
					},
					success(res){
						if (res.code == 200){
							if (that.page == 1){
								that.dataList = res.data
							} else{
								that.dataList = [...that.dataList, ...res.data]
							}
							that.showNoData = that.dataList.length < 1
							that.showKeyword = that.keyword
							
							that.hasMoreData = res.data.length >= 10
						}
						uni.stopPullDownRefresh()
					},
					failed(e){
						uni.stopPullDownRefresh()
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.header-place{
		width: 100%;
		height: 88rpx;
		background: white;
	}
	.header-box{
		display: flex;
		flex-direction: row;
		align-items: center;
		width: 100%;
		height: 88rpx;
		background: white;
		
		.back-ic{
			width: 64rpx;
			height: 64rpx;
			margin-left: 22rpx;
			margin-right: 14rpx;
		}
		
		.input-box{
			display: flex;
			flex-direction: row;
			align-items: center;
			width: 398rpx;
			height: 60rpx;
			border: #DDDDDD solid 1rpx;
			background: #F5F5F5;
			border-radius: 30rpx;
		}
	}
	
	.list-item{
		display: flex;
		flex-direction: column;
		width: calc(100% - 108rpx);
		margin: 0rpx 30rpx 20rpx 30rpx;
		padding: 24rpx;
		background: white;
		border-radius: 16rpx;
		
		.avatar{
			width: 80rpx;
			height: 80rpx;
			margin-right: 24rpx;
		}
		
		.column-line{
			width: 1rpx;
			height: 16rpx;
			background: #999999;
			margin: 0rpx 16rpx;
		}
		
		.grey-box{
			width: 150rpx;
			height: 60rpx;
			text-align: center;
			line-height: 60rpx;
			background: #F5F5F5;
			border-radius: 10rpx;
			color: #333333;
			font-size: 26rpx;
			font-weight: 600;
		}
		
	}
	
	.empty-ic{
		width: 176rpx;
		height: 176rpx;
		margin-top: 284rpx;
		margin-bottom: 20rpx;
	}
	
</style>
